package com.isunland.managesystem.adapter;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import butterknife.BindView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseButterKnifeAdapter;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.BaseParams;
import com.isunland.managesystem.entity.rDistributionMain;
import com.isunland.managesystem.ui.DeliveryDetailActivity;
import com.isunland.managesystem.ui.DeliveryDetailFragment;
import com.isunland.managesystem.ui.DeliveryListFragment;
import com.isunland.managesystem.ui.ShippingListActivity;
import com.isunland.managesystem.ui.SignDeliveryPagerActivity;
import com.isunland.managesystem.ui.SignReceiveFragment;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.widget.CustomTextView;
import java.util.ArrayList;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/4/5 15:01
 */

public class DeliveryAdapter extends BaseButterKnifeAdapter<rDistributionMain> {

  private static final int MENU_ITEM_DETAIL = 1;
  private static final int MENU_ITEM_ATTENDANCE = 2;
  private static final int MENU_ITEM_RECEIVE = 3;
  private static final int MENU_ITEM_BILL = 4;
  private final Fragment fragment;

  @SuppressWarnings("unused")
  public DeliveryAdapter(BaseVolleyActivity context, ArrayList<rDistributionMain> objects,
      Fragment fragment) {
    super(context, objects);
    this.fragment = fragment;
  }

  @Override protected int setLayoutId() {
    return R.layout.adapter_delivery_list;
  }

  @NonNull @Override protected BaseViewHolder initHolder(View convertView) {
    return new ViewHolder(convertView);
  }

  @Override
  protected void initView(rDistributionMain item, BaseViewHolder baseHolder, int position) {
    ViewHolder holder = (ViewHolder) baseHolder;

    holder.tvTitle.setText(context.getString(R.string.contractNum));
    holder.tvContent1.setText(item.getContractName());
    holder.tvContent2.setText(MyStringUtil.connect(context.getString(R.string.deliveryPerson),
        item.getDistributionStaffName()));
    holder.ivLogo.setText(MyStringUtil.substring(item.getCustomerName(), 0, 2));
    holder.tvContent3.setText(item.getStatusConfig(item.getDataStatus()).getStatusName());
    holder.tvContent3.setTextColor(
        context.getResources().getColor(item.getStatusConfig(item.getDataStatus()).getTextColor()));
    holder.tvBottom.setText(MyStringUtil.connect(context.getString(R.string.customerNameDelivery),
        item.getCustomerName()));
    holder.ivMore.setTag(item);
  }

  public void initPopMenu(View view) {
    if (!(view.getTag() instanceof rDistributionMain)) return;
    final rDistributionMain itemObj = (rDistributionMain) view.getTag();
    if (itemObj == null) return;
    //final String id = itemObj.getId();
    PopupMenu popupMenu = new PopupMenu(context, view);
    popupMenu.getMenu().add(0, MENU_ITEM_RECEIVE, 0, "签收");
    popupMenu.getMenu().add(0, MENU_ITEM_ATTENDANCE, 1, "配送签到");
    popupMenu.getMenu().add(0, MENU_ITEM_BILL, 2, "发货清单");
    popupMenu.getMenu().add(0, MENU_ITEM_DETAIL, 3, "查看详情");
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
      @Override public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
          case MENU_ITEM_DETAIL:
            Intent intent = new Intent(context, DeliveryDetailActivity.class);
            intent.putExtra(DeliveryDetailFragment.EXTRA_VALUE, itemObj);
            context.startActivityForResult(intent, DeliveryListFragment.REQUEST_UPDATE);
            return true;
          case MENU_ITEM_RECEIVE:
            //ToastUtil.show("签收");
            SignReceiveFragment.newInstance(fragment, itemObj);
            return true;
          case MENU_ITEM_ATTENDANCE:
            BaseVolleyActivity.newInstance(fragment, SignDeliveryPagerActivity.class,
                new BaseParams<>().setItem(itemObj), DeliveryListFragment.REQUEST_UPDATE);
            return true;
          case MENU_ITEM_BILL://发货清单
            BaseVolleyActivity.newInstance(fragment, ShippingListActivity.class,
                new BaseParams<>().setItem(itemObj), 0);
            return true;
        }
        return false;
      }
    });
    popupMenu.show();
  }

  protected class ViewHolder extends BaseViewHolder {

    @BindView(R.id.iv_logo) CustomTextView ivLogo;
    @BindView(R.id.tv_title) TextView tvTitle;
    @BindView(R.id.tv_content1) TextView tvContent1;
    @BindView(R.id.tv_content2) TextView tvContent2;
    @BindView(R.id.tv_content3) TextView tvContent3;
    @BindView(R.id.tv_bottom) TextView tvBottom;
    @BindView(R.id.iv_more) ImageView ivMore;
    @BindView(R.id.tv_index) TextView tvIndex;

    ViewHolder(View view) {
      super(view);
      tvIndex.setVisibility(View.GONE);

      ivMore.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {

          initPopMenu(v);
        }
      });
    }
  }
}
